<?php
namespace TennisTournament\Controller;

use TennisTournament\Helpers\ModelFactoriesHelper;
use TennisTournament\Model\StageOfTournamentEnum;
use TennisTournament\Service\MatchService;
use Zend\Json\Json;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class MatchController extends AbstractActionController {

    /** @var MatchService */
    protected $matchService;

    const FORWARD_SHOW_ALL = 'tennis-tournament/match/show-all';

    public function __construct(MatchService $matchService) {
        $this->matchService = $matchService;
    }

    public function updateAction() {

        $matchId = (int) $_POST['id'];
        $match = $this->matchService->get($matchId);
        $match = ModelFactoriesHelper::updateMatchBy($_POST, $match);

        $this->matchService->update($match);

        return $this->redirect()->toRoute('match');
    }



    public function showAllAction() {
        $finalGroupStageJSON = Json::encode($this->addMatchesDependGroupStage());


       $viewModel = new ViewModel();
       $viewModel->setTemplate(self::FORWARD_SHOW_ALL);

        return $viewModel->setVariables(array('groupStageMatchesAsJSON' => $finalGroupStageJSON));
    }

    /**
     * Add matches depend of stage of tournament
     * @return array
     */
    private function addMatchesDependGroupStage() {
        $finalGroupStageJSON = array("stageOfTournament" => array());

        $final = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::FINAL_STAGE);
        for ($i = 0; $i < count($final); $i++) {
            $final[$i] = $final[$i]->toArray();
        }

        if ($final != null) {
            array_push($finalGroupStageJSON['stageOfTournament'], array(
                    'stage' => StageOfTournamentEnum::FINAL_STAGE_VIEW,
                    'matches' => $final
                )
            );
        }

        $thirdPlaceStage = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::THIRD_PLACE);
        for ($i = 0; $i < count($thirdPlaceStage); $i++) {
            $thirdPlaceStage[$i] = $thirdPlaceStage[$i]->toArray();
        }

        if ($thirdPlaceStage != null) {
            array_push($finalGroupStageJSON['stageOfTournament'], array(
                    'stage' => StageOfTournamentEnum::THIRD_PLACE_VIEW,
                    'matches' => $thirdPlaceStage
                )
            );
        }

        $matchesSemiFinalsStage = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::SEMIFINAL);
        for ($i = 0; $i < count($matchesSemiFinalsStage); $i++) {
            $matchesSemiFinalsStage[$i] = $matchesSemiFinalsStage[$i]->toArray();
        }

        if ($matchesSemiFinalsStage != null) {
            array_push($finalGroupStageJSON['stageOfTournament'], array(
                    'stage' => StageOfTournamentEnum::SEMIFINAL_VIEW,
                    'matches' => $matchesSemiFinalsStage
                )
            );
        }

        $matchesQuarterFinalsStage = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::QUARTERFINAL);
        for ($i = 0; $i < count($matchesQuarterFinalsStage); $i++) {
            $matchesQuarterFinalsStage[$i] = $matchesQuarterFinalsStage[$i]->toArray();
        }

        if ($matchesQuarterFinalsStage != null) {
            array_push($finalGroupStageJSON['stageOfTournament'], array(
                    'stage' => StageOfTournamentEnum::QUARTERFINAL_VIEW,
                    'matches' => $matchesQuarterFinalsStage
                )
            );
        }

        $matchesSecondStage = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::SECOND_STAGE);
        for ($i = 0; $i < count($matchesSecondStage); $i++) {
            $matchesSecondStage[$i] = $matchesSecondStage[$i]->toArray();
        }

        if ($matchesSecondStage != null) {
            array_push($finalGroupStageJSON['stageOfTournament'], array(
                    'stage' => StageOfTournamentEnum::SECOND_STAGE_VIEW,
                    'matches' => $matchesSecondStage
                )
            );
        }

        $matchesGroupStage = $this->matchService->fetchAllWithStageOfTournament(StageOfTournamentEnum::GROUP_STAGE);
        for ($i = 0; $i < count($matchesGroupStage); $i++) {
            $matchesGroupStage[$i] = $matchesGroupStage[$i]->toArray();
        }

        if ($matchesGroupStage != null) {
            array_push($finalGroupStageJSON['stageOfTournament'], array(
                    'stage' => StageOfTournamentEnum::GROUP_STAGE_VIEW,
                    'matches' => $matchesGroupStage
                )
            );
        }

        return $finalGroupStageJSON;
    }

}